/*
 * @lc app=leetcode.cn id=20 lang=javascript
 *
 * [20] 有效的括号
 */

// @lc code=start
/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function(s) {
  const l = s.length;
  if (l % 2 === 1) return false;
  const stack = [];
  const symbol = new Map([
      [')', '('],
      [']', '['],
      ['}', '{']
  ])
  for(let val of s) {
      if (symbol.has(val)) {
          if (!stack.length || stack[stack.length - 1] !== symbol.get(val)) return false;
          stack.pop();
      } else {
          stack.push(val);
      }
  }
  return !stack.length
};
// @lc code=end

